package com.ethink.platform.manager;

import com.ethink.order.api.TocSaleImportJobLogFacade;
import com.ethink.order.api.info.SaleOrderInfo;
import com.ethink.order.api.info.TocSaleImportLogDetailInfo;
import com.ethink.order.api.info.TocSaleImportLogInfo;
import com.ethink.order.api.request.TocSaleImportJobLogAddRequest;
import com.ethink.platform.api.constant.PlatformConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author default
 * @version 1.0.0
 * @ClassName AsyncManager.java
 * @Description
 * @createTime 2023年02月02日 13:17:00
 */
@Component
@Slf4j
public class AsyncManager {


    @Autowired
    private SaleOrderManager saleOrderManager;

    @Autowired
    private TocSaleImportJobLogFacade tocSaleImportJobLogFacade;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 销售单失败处理
     *
     * @param main
     * @param detailInfos
     */
    @Async("defaultThreadPoolExecutor")
    public void saleAdd2FailureProcess(TocSaleImportLogInfo main, List<TocSaleImportLogDetailInfo> detailInfos) {
        Map<String, List<TocSaleImportLogDetailInfo>> tocSaleImportLogDetailInfoMap = detailInfos.stream()
                .collect(Collectors.groupingBy(TocSaleImportLogDetailInfo::getShopOrderNo));
        // 结算单内每个网店单号创建一条销售单
        for (Map.Entry<String, List<TocSaleImportLogDetailInfo>> tocSaleImportLogDetailInfoEntry : tocSaleImportLogDetailInfoMap.entrySet()) {
            String shopOrderNo = tocSaleImportLogDetailInfoEntry.getKey();
            List<TocSaleImportLogDetailInfo> list = tocSaleImportLogDetailInfoEntry.getValue();
            String subWarehouseCode = list.get(0).getSubWarehouseCode();
            String subWarehouseName = list.get(0).getSubWarehouseName();
            TocSaleImportJobLogAddRequest addRequest;
            try {
                SaleOrderInfo saleOrderInfo = saleOrderManager.saleAdd2One(shopOrderNo, subWarehouseCode, subWarehouseName, main, list);
                // 修改成功日志
                addRequest = new TocSaleImportJobLogAddRequest();
                addRequest.setLogId(main.getId());
                addRequest.setShopOrderNo(shopOrderNo);
                addRequest.setIsSuccess(1);
                addRequest.setStatus(2);
                tocSaleImportJobLogFacade.updateByShopNoAndLogId(addRequest);
            } catch (Exception e) {
                log.error("导入订单出库失败: " + shopOrderNo, e);
                //修改失败日志
                addRequest = new TocSaleImportJobLogAddRequest();
                addRequest.setLogId(main.getId());
                addRequest.setShopOrderNo(shopOrderNo);
                addRequest.setIsSuccess(0);
                addRequest.setStatus(1);
                addRequest.setFailReason(e.getMessage());
                tocSaleImportJobLogFacade.updateByShopNoAndLogId(addRequest);
            }
        }
        //删除key
        redisTemplate.opsForHash().delete(PlatformConstant.SALE_IMPORT_LOG_ID_KEY, main.getId().toString());
    }
}
